import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, roc_curve, auc
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import time
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
import pickle as pk
import joblib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn import preprocessing
import time



from sklearn import metrics
class Model:
    global y

    def __init__(self, data):
        self.data = data
        X = preprocessing.StandardScaler().fit(self.data).transform(self.data)
        self.X = X

    def DecisionTree(self):
        start_time = time.time()
        dtree = np.load('dtree.npy', allow_pickle=True).item()
        predicted_dt = dtree.predict(self.X)
        accuracy_dt = metrics.accuracy_score(y, predicted_dt)
        print(f"The Accuracy of DT is : {round(accuracy_dt * 100, 2)}%")
        print("--- %s seconds ---" % (time.time() - start_time))
        print("########################################################################")
        print(classification_report(predicted_dt, y))
        print("########################################################################")
        print("--- %s seconds ---" % (time.time() - start_time))

    def RandomForest(self):
        start_time = time.time()
        RF = joblib.load("./RF.pkl")

        predicted_rf = RF.predict(self.X)
        rf_accuracy = accuracy_score(y, predicted_rf)

        # ROC
        fpr, tpr, thresholds = roc_curve(y, predicted_rf, pos_label=None, sample_weight=None, drop_intermediate=True)
        roc_auc = auc(fpr, tpr)
        plt.subplots(figsize=(7, 5.5))
        plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
        plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title('ROC Curve')
        plt.legend(loc="lower right")
        plt.show()

        print(f"The Accuracy of RF is : {round(rf_accuracy * 100, 2)}%", '\n')
        print("--- %s seconds ---" % (time.time() - start_time))
        print("########################################################################")
        report = classification_report(predicted_rf, y, output_dict=True)
        df = pd.DataFrame(report).transpose()
        columns = df.columns.tolist()
        row = df._stat_axis.tolist()
        value = df.values.tolist()
        print(df)
        print(columns)
        print(row)
        print(value)
        print("########################################################################")

        print("--- %s seconds ---" % (time.time() - start_time))



data = pd.read_csv('dataset_sdn.csv')
df = data.copy()
df = df.dropna()

X = df.drop(['dt', 'src', 'dst', 'label'], axis=1)
y = df.label
X = pd.get_dummies(X)
M = Model(X)
M.RandomForest()


